//
//  VideoViewController.m
//  LOL_PalmarHelper
//
//  Created by dllo on 15/6/19.
//  Copyright (c) 2015年 hantao. All rights reserved.
//

#import "VideoViewController.h"

@interface VideoViewController ()

@end

@implementation VideoViewController


- (void)dealloc
{
    [_array1 release];
    [_array2 release];
    [_name1 release];
    [_name2 release];
    [_colloectionView release];
    [_flowLayout release];
    [_list release];
    [_moreVideo release];
    [_mydic release];
    [_myview release];
    [_hud release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    

    NSString *path1 = [[NSBundle mainBundle] pathForResource:@"iconfont-moredrawdown" ofType:@"png"];
   
    UIImage *image1 = [UIImage imageWithContentsOfFile:path1];
    image1 = [image1 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];//取消渲染
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:image1 style:UIBarButtonItemStyleDone target:self action:@selector(buAction)];

    
    self.bu = NO;
    
    
    
    
    
    
    self.navigationItem.title = @"视频导航";
//    self.navigationItem.titleView = [[UIView alloc] initWithFrame:CGRectMake(30, 20, 250, 44)];
//    self.navigationItem.titleView.center = self.navigationController.navigationBar.center;
//    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 250, 40)];
//    label1.text = @"视频导航";
//    label1.textAlignment = NSTextAlignmentCenter;
//    [self.navigationItem.titleView addSubview:label1];
//    label1.textColor = [UIColor cyanColor];
//    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
        
    
    
    self.mydic = [NSMutableDictionary dictionary];
    
    self.array1 = [NSMutableArray array];
    
    self.array2 = [NSMutableArray array];
    [self getmyData];
    
    
    
    // UICollectionViewLayout是一个抽象的类。我们不用它来创建对象而是用它的子类
    
    //  UICollectionViewFlowLayout 是苹果自带的一个瀑布流类
    
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    //   设置Items大小
    self.flowLayout.itemSize = CGSizeMake(80, 100);
    
   
    //    头部视图大小
    self.flowLayout.headerReferenceSize = CGSizeMake(self.view.bounds.size.width, 50);
    
    //    //     底部区域视图大小
    //
    self.flowLayout.footerReferenceSize =  CGSizeMake(self.view.bounds.size.width, 0);
    //
    //    水平滚动
    //    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    //    垂直滚动
    self.flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    self.flowLayout.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);//设置section的边界
    
    
    //   两列之间的间距
         self.flowLayout.minimumInteritemSpacing = 10;
    
    //    UIEdgeInsets UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right)
    
    [_flowLayout release];
    
    
    
    
    //  集合视图
    
    self.colloectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.bounds.size.height - 110) collectionViewLayout:self.flowLayout];
    
    self.colloectionView.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.colloectionView];
    
    self.colloectionView.delegate = self;
    
    self.colloectionView.dataSource = self;
    
    //    禁止边界反弹
    
//    self.colloectionView.bounces = NO;
    
    
    
    //    注册UICollectionViewCell用到系统的要注册
    
    [self.colloectionView registerClass:[VideoCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    
    //    头部
    [self.colloectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerReuse"];
    
    //    底部
    
    [self.colloectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerReuse"];

    
    [_colloectionView release];
    
    
    //
    self.myview = [[UIView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width, 64, 50, 48)];
    
    
    //    myview.backgroundColor = [UIColor lightGrayColor];
    [self.navigationController.view addSubview:self.myview];
    
    UIImageView *view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.myview.frame.size.width, self.myview.frame.size.height)];
    view.backgroundColor = [UIColor orangeColor];
    view.image = [UIImage imageNamed:@"navbar1.png"];
    [self.myview addSubview:view];
    
    [view release];
    
  
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    button.frame = CGRectMake(0, 0, self.myview.frame.size.width, self.myview.frame.size.height / 2);
    
    [button setTitle:@"最新" forState:UIControlStateNormal];
    
    [self.myview addSubview:button];
    
    [button addTarget:self action:@selector(buB) forControlEvents:UIControlEventTouchUpInside];
    
    button.titleLabel.font = [UIFont systemFontOfSize:13];
    
    button.titleLabel.textColor = [UIColor greenColor];
    
    
    
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(3, 0, 44, 1)];
    
    view1.backgroundColor = [UIColor whiteColor];
    
    [self.myview addSubview:view1];
    
    [view1 release];
    
    
    
    
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    
    button1.frame = CGRectMake(0, self.myview.frame.size.height / 2, self.myview.frame.size.width, self.myview.frame.size.height / 2);
    
    [button1 setTitle:@"英雄" forState:UIControlStateNormal];
    
    [button1 addTarget:self action:@selector(buB1) forControlEvents:UIControlEventTouchUpInside];
    
    [button1 setTintColor:[UIColor redColor]];
    
    button1.titleLabel.font = [UIFont systemFontOfSize:13];
    
    [self.myview addSubview:button1];
    
    
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(3, self.myview.frame.size.height / 2, 44, 1)];
    
    view2.backgroundColor = [UIColor whiteColor];
    
    [self.myview addSubview:view2];
    
    [view2 release];
    
    
    
    
    
    
    self.hud = [[MBProgressHUD alloc] initWithView:self.view];
    
    [self.view addSubview:self.hud];
    
    self.hud.labelText = @"HHZ小组正卖力加载中";
    
    [self.hud show:YES];
    
    [_hud release];
    
    
    [_myview release];
    
    
    // 设置回调（一旦进入刷新状态，就调用target的action，也就是调用self的loadNewData方法）
    self.colloectionView.header =  [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];



       
}





// *************************************

- (void)loadNewData
{
    [self getmyData];
}

- (void)buAction
{

    if (self.bu == NO) {
    
        
    //    动画效果
    
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.2 initialSpringVelocity:0.5 options:UIViewAnimationOptionLayoutSubviews animations:^{
        
        self.myview.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - 51, 64, 50, 48);
        
    } completion:^(BOOL finished) {
        
        self.bu = YES;
        
   
        
         }];
        
     }
    
    
   else if (self.bu == YES) {
        
        
        //    动画效果
        
        [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.2 initialSpringVelocity:0.5 options:UIViewAnimationOptionLayoutSubviews animations:^{
            
            self.myview.frame = CGRectMake(self.view.frame.size.width, 64, 50, 48);
            
        } completion:^(BOOL finished) {
            
            self.bu = NO;
    
            
        }];
        
    }

    
    
  
    
  }



- (void)buB
{
    
    LastMovieViewController *last = [[LastMovieViewController alloc] init];
    
    UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:last];
    
    [self presentViewController:naVC animated:YES completion:^{
        
    }];

    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:0.5 options:UIViewAnimationOptionLayoutSubviews animations:^{
        
        self.myview.frame = CGRectMake(self.view.frame.size.width, 64, 50, 48);
        
    } completion:^(BOOL finished) {
        
        self.bu = NO;
        
        
    }];

    
    
    
    
}

- (void)buB1
{
    self.moreVideo = [[MoreVideoViewController alloc] init];
    UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:self.moreVideo];
    
    [self presentViewController:naVC animated:YES completion:^{
        
    }];
    
    
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:0.5 options:UIViewAnimationOptionLayoutSubviews animations:^{
        
        self.myview.frame = CGRectMake(self.view.frame.size.width, 64, 50, 48);
        
    } completion:^(BOOL finished) {
        
        self.bu = NO;
        
        
    }];

    
}




// 点击某个Item

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    
    
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:0.5 options:UIViewAnimationOptionLayoutSubviews animations:^{
        
        self.myview.frame = CGRectMake(self.view.frame.size.width, 64, 50, 48);
        
    } completion:^(BOOL finished) {
        
        self.bu = NO;
        
        
    }];
    

    self.list = [[VideoListViewController alloc] init];
    
    NSString *str = [self.mydic.allKeys objectAtIndex:indexPath.section];
    
    NSArray *arr = [self.mydic objectForKey:str];
    
    VideoModel *myda = [arr objectAtIndex:indexPath.row];
    
    self.list.tag = myda.tag;
    self.list.heroname = myda.name;
    
    [self.navigationController pushViewController:self.list animated:YES];
    [_list release];
    
    
    
    
    
}


//  设置分区section

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    
    return self.mydic.allKeys.count;

    
}

// Items代表小框的个数

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    self.name1 = [self.mydic.allKeys objectAtIndex:section];
    
    NSArray *arr = [self.mydic objectForKey:self.name1];
    
    
    
    return arr.count;
    
}

// 布局cell
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //    直接从系统的重用池里取cell
    
    
    //    自定义cell
    VideoCollectionViewCell *mycell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
//    mycell.layer.transform = CATransform3DMakeScale(0.3, 0.3, 0.1);
//    [UIView animateWithDuration:0.5 animations:^{
//        
//        mycell.layer.transform = CATransform3DMakeScale(1, 1, 0.1);
//       
//        
//
//    }];
//

    NSString *str = [self.mydic.allKeys objectAtIndex:indexPath.section];
    
    NSArray *arr = [self.mydic objectForKey:str];
    
    mycell.VideoData = [arr objectAtIndex:indexPath.row];
    
    
    return mycell;
}


// 设置表头和底部区域内容

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    //    kind 是显示区域类型
    if (kind == UICollectionElementKindSectionHeader) {
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"headerReuse" forIndexPath:indexPath];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 300, 50)];
        label.numberOfLines = 1;
        label.font = [UIFont systemFontOfSize:20];
        label.textAlignment = NSTextAlignmentLeft;
        
        [headerView addSubview:label];
        
        [label release];
        label.backgroundColor = [UIColor whiteColor];
        
        if (indexPath.section == 0) {
            
            [headerView addSubview:label];
            
            label.text = [self.mydic.allKeys objectAtIndex:0];
            
            label.backgroundColor = [UIColor whiteColor];
            
            return headerView;
            
        }
        if (indexPath.section == 1)
        {
            
            [headerView addSubview:label];
            label.text = [self.mydic.allKeys objectAtIndex:1];
            
            label.backgroundColor = [UIColor whiteColor];
            
            return headerView;
        }
        
        
        else if (indexPath.section == 2)
        {
            [headerView addSubview:label];
            label.text = [self.mydic.allKeys objectAtIndex:2];
            
            label.backgroundColor = [UIColor whiteColor];
            return headerView;
        }
        
        else if (indexPath.section == 3)
        {
            [headerView addSubview:label];
            label.text = [self.mydic.allKeys objectAtIndex:3];
            
            label.backgroundColor = [UIColor whiteColor];
            return headerView;
            
        }
        
        
        else if (indexPath.section == 4)
        {
            
            [headerView addSubview:label];
            label.text = [self.mydic.allKeys objectAtIndex:4];
            
            label.backgroundColor = [UIColor whiteColor];
            
            
        }
        
        
        return headerView;
        
    }
    else
    {
        UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footerReuse" forIndexPath:indexPath];
        footerView.backgroundColor = [UIColor whiteColor];
        return footerView;
    }
    
    
    
}



























- (void)getmyData
{
    
    
    [[HeroBaseHandle ShareHeroHeroDate] openDB];
    
   NSArray *array = [[HeroBaseHandle ShareHeroHeroDate] selectAllmyVideo];
    
    if (array.count != 0) {
        
    
    NSMutableArray *arr = [NSMutableArray array];
    
    NSMutableArray *arr1 = [NSMutableArray array];
    
    NSMutableArray *arr2= [NSMutableArray array];
    
    NSMutableArray *arr3 = [NSMutableArray array];
    
    NSMutableArray *arr4 = [NSMutableArray array];
    
    for (VideoModel *video in array) {
         if ([video.myname isEqualToString:@"推荐视频"]) {
                     [arr addObject:video];
                   }
        
        if ([video.myname isEqualToString:@"解说视频"]) {
         [arr1 addObject:video];
        }

        if ([video.myname isEqualToString:@"搞笑视频"]) {
       [arr2 addObject:video];
        }
 
        if ([video.myname isEqualToString:@"综合视频"]) {
      [arr3 addObject:video];
            
        }
      
        if ([video.myname isEqualToString:@"英雄视频"]) {
     [arr4 addObject:video];
            
        }
         [self.mydic setObject:arr forKey:@"推荐视频"];
         [self.mydic setObject:arr1 forKey:@"解说视频"];
         [self.mydic setObject:arr2 forKey:@"搞笑视频"];
         [self.mydic setObject:arr3 forKey:@"综合视频"];
         [self.mydic setObject:arr4 forKey:@"英雄视频"];
     
}
    
}
    
    

   
    
   [AFN AFNGetData:@"http://box.dwstatic.com/apiVideoesNormalDuowan.php?src=duowan&action=c&sk=&sn=&pn=" andBlock:^(id respondes) {
    
       if (![respondes isKindOfClass:[@"error" class]]) {

           
           
           [[HeroBaseHandle ShareHeroHeroDate] dropmyVideoTable];
           
           [[HeroBaseHandle ShareHeroHeroDate] createmyVideoTable];
           
           [self.mydic removeAllObjects];

          
           NSArray *array = respondes;
           
           
           for (NSDictionary *dic1 in array) {
               
               NSMutableArray *arr = [NSMutableArray array];
               
               self.name1 = [dic1 objectForKey:@"name"];
               
               NSArray *arr1 = [dic1 objectForKey:@"subCategory"];
               
               for (NSDictionary *dic2 in arr1) {
                   
                   VideoModel *data = [[VideoModel alloc] init];
                   
                   data.tag = [dic2 objectForKey:@"tag"];
                   
                   data.name = [dic2 objectForKey:@"name"];
                   
                   data.icon = [dic2 objectForKey:@"icon"];
                   
                   [[HeroBaseHandle ShareHeroHeroDate] insertmyVideo:data myName:self.name1];
                   
                   
                   
                  [arr addObject:data];
                   
                   
                   [data release];
               }
               
               [_hud hide:YES];
               
               [self.mydic setObject:arr forKey:self.name1];
               
               
           }
         
           [self.colloectionView reloadData];
           [self.colloectionView.header endRefreshing];
           
       }else if ([respondes isEqualToString:@"error"]) {
           
           [_hud hide:YES];
           
           UIAlertView *al = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络不给力" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
           [self.view addSubview:al];
           [al show];
           [al release];
           
       }

       [self.colloectionView.header endRefreshing];

       
   }];
    

    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
